package stud.mpgame.server.preview;

import org.apache.mina.core.session.IoSession;

import stud.mpgame.net.command.ReadyToGameCommand;

import stud.mpgame.net.description.LoginMessageDescription;
import stud.mpgame.net.description.MapInfoMessageDescription;
import stud.mpgame.net.description.ReadyToGameMessageDescription;

import stud.mpgame.net.userobject.MapInfoUserObject;

import stud.mpgame.network.IMessageReceivedListener;
import stud.mpgame.network.Message;
import stud.mpgame.network.MessageDescription;
import stud.mpgame.network.NetworkMonitor;

import stud.mpgame.network.util.MessageFilter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author f.pavel91@gmail.com
 * @version 1.0
 */
public class PreviewGameManager {

    private List < IoSession > readySessions;

    private NetworkMonitor networkMonitor;

    public PreviewGameManager(NetworkMonitor networkMonitor) {
        this.networkMonitor = networkMonitor;
        this.readySessions = new ArrayList< IoSession >();

        //register descriptions
        MessageDescription.registerDescription(LoginMessageDescription.getInstance());
        MessageDescription.registerDescription(MapInfoMessageDescription.getInstance());
        MessageDescription.registerDescription(ReadyToGameMessageDescription.getInstance());

        addLoginMessageListener();
        addReadyToGameMessageListener();

    }

    private void addLoginMessageListener() {
        MessageFilter messageFilter = new MessageFilter(LoginMessageDescription.getInstance());

        networkMonitor.getMessageDispatcher().addMessageReceivedListener(messageFilter,
                new IMessageReceivedListener() {
                    public void messageReceived(Message message) {
                        System.out.println("LOGIN RECEIVED = " + message.getReceivedTime() + "NOW = " + networkMonitor.nowTime());

                        Message mapInfoMessage = new Message(message.getFirstSession(), MapInfoMessageDescription.getInstance());
						MapInfoUserObject mapInfoUserObject = new MapInfoUserObject();
						mapInfoMessage.putUserObject(mapInfoUserObject);
						mapInfoMessage.send();
                        System.out.println("Map info sent");
                    }
                }
        );
    }

    private void addReadyToGameMessageListener() {
        MessageFilter messageFilter = new MessageFilter(ReadyToGameMessageDescription.getInstance());

        networkMonitor.getMessageDispatcher().addMessageReceivedListener(messageFilter,
                new IMessageReceivedListener() {
                    public void messageReceived(Message message) {
						readySessions.add(message.getFirstSession());
                        ReadyToGameCommand readyToGameCommand = (ReadyToGameCommand) message.getUserObject(ReadyToGameCommand.class);
                        message.getFirstSession().setAttribute("userID", readyToGameCommand.getUserID());
                        System.out.println("READY");
                    }
                }
        );
    }

    public List<IoSession> getReadySessions() {
        return readySessions;
    }

}
